package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.SupDetailMapper;
import cn.tedu.csmall.product.pojo.dto.SupDetailAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.SupDetail;
import cn.tedu.csmall.product.pojo.vo.SupDetailStandardVO;
import cn.tedu.csmall.product.service.ISupDetailServuce;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SupDetailServuceImpl implements ISupDetailServuce, ServiceCode {
    @Autowired
    SupDetailMapper supDetailMapper;
    public SupDetailServuceImpl() {
        log.debug("创建业务类:SupDetailServuceImpl");
    }

    @Override
    public void andNew(SupDetailAndNewDTO supDetailAndNewDTO) {
        log.debug("开始处理添加业务：参数{}",supDetailAndNewDTO);
        SupDetail supDetail=new SupDetail();
        LocalDateTime now=LocalDateTime.now();
        BeanUtils.copyProperties(supDetailAndNewDTO,supDetail);
        supDetail.setGmtCreate(now);
        supDetail.setGmtModified(now);
        int insert = supDetailMapper.insert(supDetail);
        if (insert!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
        log.debug("开始处理删除业务：参数{}",id);
        SupDetailStandardVO supDetail = supDetailMapper.getSupDetail(id);
        if (supDetail==null){
            String message="删除数据失败！"+id+"该数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int delete = supDetailMapper.delete(id);
             if (delete!=1){
                 String message="删除数据失败！服务器忙，请稍后再一次尝试!";
                 log.debug(message);
                 throw new ServiceException(ServiceCode.ERR_DELETE,message);
             }
    }
}
